//
//  XYJSettingViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/16.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJSettingViewController.h"
#import "XYJAboutUsViewController.h"
#import "XYJKYViewController.h"
#import "XYJJYViewController.h"
#import "SZKImagePickerVC.h"
#import "ClearCache.h"

@interface XYJSettingViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *_tableView;
}

@end

@implementation XYJSettingViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initTable];
}


-(void)initTable{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 4;
    }
    else return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = [NSString stringWithFormat:@"cellID%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSInteger count = indexPath.section + indexPath.row;
    NSArray *titleText = @[@"清除缓存",@"应用评分",@"关于我们",@"开元许可",@"问题反馈",@"注销登录"];
    if (indexPath.section == 2) {
        cell.textLabel.text = @"注销登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor redColor];
    }else{
        cell.textLabel.text = titleText[count];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
    }
    cell.separatorInset = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}



#pragma mark - UITableViewDelegate
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

//Header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

//footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}

//cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld%ld",(long)indexPath.section,(long)indexPath.row);
    //关于我们
    if (indexPath.section == 1 && indexPath.row == 1) {
        XYJAboutUsViewController *VC = [[XYJAboutUsViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    //开元许可
    if (indexPath.section == 1 && indexPath.row == 2) {
        XYJKYViewController *VC = [[XYJKYViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    //建议
    if (indexPath.section == 1 && indexPath.row == 3) {
        XYJJYViewController *VC = [[XYJJYViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    //应用评分
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly:@NO};
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/chan-you-ji-2014nian-lu-xing/id559653959?mt=8"] options:options completionHandler:nil];
    }
    //清除缓存
    if (indexPath.section == 0) {
        [self alert];
    }
    //退出登录
    if (indexPath.section == 2) {
        [self quiet];
    }
}

#pragma mark - 退出点击事件
-(void)quiet{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您确定要退出吗?"] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //清除头像
        [SZKImagePickerVC clearHeaderImg];
        //清除缓存
        [ClearCache removedCache];
        //添加通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clearIconImg" object:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:sureAction];
    [alertC addAction:cancelAction];
    [self.navigationController presentViewController:alertC animated:YES completion:nil];
}


#pragma mark - alert提示方法
- (void)alert{
    
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您确定要清空%.2fM缓存吗?",[ClearCache clearCache]] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //清除头像
        [SZKImagePickerVC clearHeaderImg];
        //清除缓存
        [ClearCache removedCache];
        //添加通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clearIconImg" object:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    }];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
    
}


 


@end
